package br.com.extrajudicial.models;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.validator.NotNull;

@Entity
@Table(name="orders")
public class Order implements Serializable {

	private static final long serialVersionUID = -5396978876170952750L;
	
	@Id
	@GeneratedValue
	private Long id;
	@Column(name="is_completed") @NotNull
	private Boolean completed;
	private String info;
	
	@JoinColumn (name = "user_id", referencedColumnName = "id")
	@ManyToOne (optional = false)
	private User user;
	
	@JoinColumn (name = "address_id", referencedColumnName = "id")
	@ManyToOne (optional = false)
	private Address address;
	
	@OneToMany (cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "order")
	private List<Payment> payments;

	@OneToMany (cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "order")
	private List<ItemOrder> itemsOrders;

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Boolean getCompleted() {
		return completed;
	}
	public void setCompleted(Boolean completed) {
		this.completed = completed;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public List<Payment> getPayments() {
		return payments;
	}
	public void setPayments(List<Payment> payments) {
		this.payments = payments;
	}
	public List<ItemOrder> getItemsOrders() {
		return itemsOrders;
	}
	public void setItemsOrders(List<ItemOrder> itemsOrders) {
		this.itemsOrders = itemsOrders;
	}
	
	public Integer getTotalItems() {
		return this.getItemsOrders().size();
	}
	
	public BigDecimal getTotalPrice() {
		Double totalPrice = 0.0;
		for (ItemOrder itemOrder : this.getItemsOrders()) {
			totalPrice += itemOrder.getQuantity() * itemOrder.getItem().getUnityPrice().doubleValue();
		}
		return new BigDecimal(totalPrice);
	}
	
}
